﻿using DevelopAssistant.Core.ToolBar;
using DevelopAssistant.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.Core.ToolBox
{
    public partial class EditSqlServerConnection : EditDataBaseConnection
    {
        public EditSqlServerConnection()
        {
            InitializeComponent();
            InitializeControls();
        }

        public EditSqlServerConnection(DataBaseServer dbserver,LeftMenuBar menubar)
            : this()
        {
            //this.Text = "dbo." + dbserver.DataBaseName;
            this.combServer.Text = dbserver.Server;
            this.combDataBases.Items.Add(dbserver.DataBaseName);
            this.combDataBases.SelectedIndex = 0;
            this.combDataBases.Text = dbserver.DataBaseName;
            this.combDataBases.Enabled = false;
            this.txtInstance.Text =dbserver.Instance_Name;
            if (!string.IsNullOrEmpty(dbserver.Instance_Name))
            {
                this.chkInstance.Checked = true;
                this.txtInstance.Enabled = true;
            }
            this.txtUser_ID.Text = dbserver.UserID;
            this.txtPassword.Text = dbserver.Password;
            this.txtPort.Text = string.IsNullOrEmpty(dbserver.Port) ? "默认" : dbserver.Port;
            if (dbserver.DataBaseLoginStyle == DataBaseLoginStyle.Integrated_Security)
            {
                this.chkLoginStyle.Checked = true;
            }
            else
            {
                this.chkLoginStyle.Checked = false;
            }
        }

        private void InitializeControls()
        {
            SetBtnApplyEnabled(false);
            this.chkLoginStyle .CheckedChanged +=chkLoginStyle_CheckedChanged;
            this.chkInstance.CheckedChanged +=chkInstance_CheckedChanged;
        }

        private void chkLoginStyle_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLoginStyle.Checked)
            {
                gboxLoginPanel.Enabled = false;
            }
            else
            {
                gboxLoginPanel.Enabled = true;
            }
        }

        private void chkInstance_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInstance.Checked)
            {
                txtInstance.Enabled = true;
                txtInstance.ReadOnly = false;
            }
            else
            {
                txtInstance.Enabled = false;
                txtInstance.ReadOnly = true;
            }
        }

        private void label8_Click(object sender, EventArgs e)
        {
            if (txtPassword.PasswordChar=='*')
            {
                txtPassword.PasswordChar = txtUser_ID.PasswordChar;
                label8.Image = DevelopAssistant.Core.Properties.Resources.password;
                //txtPassword.UseSystemPasswordChar = false;
            }
            else
            {
                txtPassword.PasswordChar = '*';
                label8.Image = DevelopAssistant.Core.Properties.Resources.eye_scret;
                //txtPassword.UseSystemPasswordChar = true;
            }
        }

    }
}
